<template>
  <div class="site-wrapper site-page--login">
    <div class="site-content__wrapper">
      <div class="site-content">
        <div class="brand-info">

        </div>
        <div class="login-main">
          <h3 class="login-title">登陆</h3>
          <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRoles" status-icon
                   @keyup.enter.native="login"
          >
            <el-form-item prop="username">
              <el-input v-model="loginForm.username" placeholder="请输入用户名"></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input v-model="loginForm.password" placeholder="密码" type="password"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button class="login-btn-submit" type="primary" @click="login">登录</el-button>
            </el-form-item>
            <el-form-item>
              <el-button class="login-btn-submit" style="margin-top: 8px" type="primary" @click="register">注册</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>


  </div>

</template>

<script>

import { setToken } from '@/utils/auth'
import { login1 } from '@/api/boot/api'

export default {
  data() {
    return {
      //这是登录表单的数据绑定对象
      loginForm: {
        username: '',
        password: '',
        code: '',
        rememberMe: false
      },

      //这是表单的验证规则对象
      loginFormRoles: {
        //验证用户名是否合法
        username: [
          { required: true, message: '请输入登录名称', trigger: 'blur' },
          { min: 3, max: 12, message: '长度在3到10个字符', trigger: 'blur' }
        ],
        //验证密码是否合法
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在3到15个字符', trigger: 'blur' }
        ]
      },
      smsLoginRules: {
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { min: 11, max: 12, message: '格式错误' }
        ]
      }
    }
  },

  methods: {
    register(){
      this.$router.push({ path: '/register', replace: true })
    },
    login() {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return
        // const loading = this.$loading({
        //   lock: true,
        //   text: 'Loading',
        //   spinner: 'el-icon-loading',
        //   background: 'rgba(0, 0, 0, 0.7)'
        // });
        // const res = await this.postKeyValueRequest('/login1', this.loginForm);
        login1(this.loginForm).then((res) => {
          if (res.code == 0) {
            window.sessionStorage.setItem('user', JSON.stringify(res.obj))
            window.sessionStorage.setItem('token', res.token)

            if (res.rid == 3) {
              localStorage.setItem('role', 'admin')
            } else {
              localStorage.setItem('role', 'visitor')
            }

            setToken(res.token)
            this.$store.commit('changeState', res.obj)
            // loading.close();
            this.$router.push({ path: '/home', replace: true })
          } else {
            // loading.close();
            this.msgError(res.msg)
            return false
          }
        })

      })
    }
  }
}
</script>
<!--scoped如果加上只在本页面生效，去掉就全局生效,只要是但文件就定义-->
<style lang="less" scoped>
.site-wrapper.site-page--login {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background-color: rgba(38, 50, 56, .6);
  overflow: hidden;

  &:before {
    position: fixed;
    top: 0;
    left: 0;
    z-index: -1;
    width: 100%;
    height: 100%;
    content: "";
    background-image: url(~@/assets/img/login_bg.jpg);
    background-size: cover;
  }

  .site-content__wrapper {

    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    padding: 0;
    margin: 0;
    overflow-x: hidden;
    overflow-y: auto;
    background-color: transparent;
  }

  .site-content {
    min-height: 100%;
    padding: 30px 500px 30px 30px;
  }

  .brand-info {
    margin: 220px 100px 0 90px;
    color: #fff;
  }

  .brand-info__text {
    margin: 0 0 22px 0;
    font-size: 48px;
    font-weight: 400;
    text-transform: uppercase;
  }

  .brand-info__intro {
    margin: 10px 0;
    font-size: 16px;
    line-height: 1.58;
    opacity: .6;
  }

  .login-main {
    position: absolute;
    top: 20%;
    right: 40%;
    //padding: 150px 60px 180px;
    width: 20%;
    min-height: 100%;
    background-color: Transparent;
  }

  .login-title {
    font-size: 25px;
    color: #fff;
  }

  .login-captcha {
    overflow: hidden;

    > img {
      width: 100%;
      cursor: pointer;
    }
  }

  .login-btn-submit {
    width: 100%;
    margin-top: 28px;
  }

}

</style>
